package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.view.ExpandHeadView;

/**
 * Created by Administrator on 2017/7/21 0021.
 *
 * 维修保养方案接h5界面
 */
public class BaoYangAcitivity  extends BaseActivity{

    public ExpandHeadView mHeadView;
    public WebView mWebView;
    public String param;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_baoyang;
    }

    @Override
    protected void initView() {
        mHeadView = (ExpandHeadView) findViewById(R.id.baoyang_head);
        mWebView = (WebView) findViewById(R.id.webview);
        setSettings(mWebView);
    }

    @Override
    protected void initData() {
        param = getIntent().getStringExtra("param");
        mWebView.loadUrl("file:///android_asset/html/maintenance-record.html");
        mWebView.addJavascriptInterface(new ProviderForJavaScript(), "connectedBridge");
    }

    @Override
    protected void initListener() {
        mHeadView.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void setSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAppCacheEnabled(false);
        webView.setWebChromeClient(new WebChromeClient());
    }

    private class ProviderForJavaScript {
        @JavascriptInterface
        public void sendParam() {
            mWebView.post(new Runnable() {
                @Override
                public void run() {
                    ALog.d(param);
                    mWebView.loadUrl("javascript:vm.init(" + param + ")");
                }
            });
        }
    }
}
